<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Site {
    
    public function Initialize() {
        global $config, $cms_config, $timers;
        
        $cms_config = $this->Getc('cms_config');
        $timers = $this->Getc('cms_timers');
        
        $site_config = $config[1];
        
        define('WWW', $site_config['www']);
        define('HTTPS', ($site_config['https'] ? (str_replace('http', 'https', WWW)) : WWW));
        define('WEB_GALLERY', $this->getGallery($site_config['auto_gallery']));
        
    }
    public  function Getc($table) {
        $sql = query('SELECT var,result FROM ' . $table);

        while($row = mysql_fetch_assoc($sql))
        {
            $i = 0;
            foreach($row as $param => $value)
            {
                $i++;

                if($i == 1)
                        $p = $value;
                else
                        $v = $value;
            }
            $site[$p] = $v;
        }
	return $site;
    }
    public function CheckLogin($username, $password){
        if(empty($username) && empty($password))
            return '|false||';
        elseif(empty($password))
            return '|false||true|';
        elseif(empty($username))
            return '|false||';
        elseif(!$this->CorrectLogin($username, $password))
            return '|false||';
        else 
            return true;
    }
    public function SecurePass($password){
        return sha1(md5($password));
    }
    public function GetFromCheck($input, $num){
        $text = @explode('|', $input);
        $text = @explode('|', $text[$num]);
        return $text[0];
    }
    public function No($input){
        return mysql_real_escape_string($input);
    }
    public function IsFocus($input){
        $check = $this->GetFromCheck($input, 3);
        if(!empty($check))
            return ' focus';
        else
            return '';
    }
    public function getGallery($auto) {
        global $cms_config, $timers;
        
        if($auto) {
            $gallery = $cms_config['web_build'];
            
            if(empty($cms_config['web_build']) || empty($cms_config['last_update_build']) || time() > $timers['update_gallery'] + $cms_config['last_update_build']) {
                
                $build = $this->GetBuild();
                $build_version = $this->GetBuild(false);
                
                query("UPDATE cms_config SET result='$build' WHERE var = 'web_build'");
                query("UPDATE cms_config SET result='$build_version' WHERE var = 'web_build_version'");
                query("UPDATE cms_config SET result='" . time() . "' WHERE var = 'last_update_build'");
                
                $gallery = $build;
            }
            
            return 'http://images.habbo.com/habboweb/' . $gallery . '/web-gallery';
        }
        else {
            return WWW . '/web-gallery';
        }
        
    }
    public function GetBuild($e = true) {
        if($e) {
            $var1 = 'var habboStaticFilePath = "http://images.habbo.com/habboweb/';
            $var2 = '/web-gallery";';
        }
        else {
            $var1 = '<meta name="build" content="';
            $var2 = '" />';
        }

        $web = file_get_contents('http://www.habbo.com');

        $buffer = explode($var1, $web);

        foreach($buffer as $data)  {
                $tmp = explode($var2, $data);
        }

        return $tmp[0];
    }
}
?>
